package com.lkd.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.Strings;
import com.lkd.dao.NodeDao;
import com.lkd.entity.BusinessTypeEntity;
import com.lkd.entity.NodeEntity;
import com.lkd.entity.RegionEntity;
import com.lkd.entity.VendingMachineEntity;
import com.lkd.exception.LogicException;
import com.lkd.http.controller.vo.NodeReq;
import com.lkd.service.BusinessTypeService;
import com.lkd.service.NodeService;
import com.lkd.service.RegionService;
import com.lkd.service.VendingMachineService;
import com.lkd.vo.Pager;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

@Slf4j
@Service
public class NodeServiceImpl  extends ServiceImpl<NodeDao, NodeEntity> implements NodeService {


    /**
     * 分页查询
     * @param pageIndex
     * @param pageSize
     * @param name
     * @param regionId
     * @return
     */
    @Override
    public Pager<NodeEntity> search(Long pageIndex, Long pageSize, String name, Long regionId) {

//        参数的判断
        if(ObjectUtils.isEmpty(pageIndex)|| pageIndex<1){
            pageIndex = Pager.DEFAULT_PAGE_INDEX;
        }

        if(ObjectUtils.isEmpty(pageSize) || pageSize<1){
            pageSize = Pager.DEFAULT_PAGE_SIZE;
        }
//        设置分页条件
        Page<NodeEntity> ipage = new Page<>(pageIndex, pageSize);
//        设置查询条件
        LambdaQueryWrapper<NodeEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .like(StringUtils.isNotBlank(name),NodeEntity::getName,name)
                .eq(regionId != null,NodeEntity::getRegionId,regionId);
//        分页查询
        Page<NodeEntity> nodeEntityPage = page(ipage, queryWrapper);
//        构造返回对象
        return Pager.build(nodeEntityPage);
    }

    @Autowired
    private RegionService regionService;
    @Autowired
    private BusinessTypeService businessTypeService;
    /**
     * 新增
     * @param nodeReq
     * @return
     */
    @Override
    public Boolean saveNode(NodeReq nodeReq) {

//        验证区域是否存在
        RegionEntity regionEntity = regionService.getById(nodeReq.getRegionId());
        if(regionEntity == null){
            log.error("区域不存在");
            throw new LogicException("区域错误");
        }
//        验证商圈是否存在
        BusinessTypeEntity businessTypeEntity = businessTypeService.getById(nodeReq.getBusinessId());
        if(businessTypeEntity == null){
            log.error("商圈不存在");
            throw new LogicException("商圈错误");
        }
//        查询名称是否重复
        LambdaQueryWrapper<NodeEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(NodeEntity::getName,nodeReq.getName());
        int count = count(queryWrapper);
        if(count>0){
            log.error("name已经存在");
            throw new LogicException("name已经存在");
        }

        NodeEntity nodeEntity = new NodeEntity();
        BeanUtils.copyProperties(nodeReq,nodeEntity);
        log.info("node={}",nodeEntity);
//        点位的保存
        return save(nodeEntity);

    }

    @Override
    public Boolean updateNode(NodeReq nodeReq, Long nodeId) {
        //        验证区域是否存在
        RegionEntity regionEntity = regionService.getById(nodeReq.getRegionId());
        if(regionEntity == null){
            log.error("区域不存在");
            throw new LogicException("区域错误");
        }
//        验证商圈是否存在
        BusinessTypeEntity businessTypeEntity = businessTypeService.getById(nodeReq.getBusinessId());
        if(businessTypeEntity == null){
            log.error("商圈不存在");
            throw new LogicException("商圈错误");
        }
//        查询名称是否重复
//        select * from tb_node where name=? and id <> ?
        LambdaQueryWrapper<NodeEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(NodeEntity::getName,nodeReq.getName())
                .ne(NodeEntity::getId,nodeId);
        int count = count(queryWrapper);
        if(count>0){
            log.error("name已经存在");
            throw new LogicException("name已经存在");
        }

        NodeEntity nodeEntity = new NodeEntity();
        BeanUtils.copyProperties(nodeReq,nodeEntity);
        nodeEntity.setId(nodeId);
        log.info("node={}",nodeEntity);
//        点位的保存
        return updateById(nodeEntity);
    }

    @Autowired
    private VendingMachineService machineService;

    /**
     * 删除
     * @param nodeId
     * @return
     */
    @Override
    public Boolean del(Long nodeId) {

//        判断点位下是否有售货机
        LambdaQueryWrapper<VendingMachineEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(VendingMachineEntity::getNodeId,nodeId);
        int count = machineService.count(queryWrapper);
        if(count>0){
            log.error("点位下存在售货机，不能删除");
            throw new LogicException("点位下存在售货机，不能删除");
        }
        return removeById(nodeId);
    }
}
